package nc.ui.med.pub.query;

import java.io.Serializable;
import java.util.ArrayList;

import nc.ui.pub.beans.constenum.IConstEnum;
import nc.ui.querytemplate.datapower.PowerCorrelationVO;
import nc.ui.querytemplate.filtereditor.DefaultFilterEditor;

@SuppressWarnings( { "unchecked", "serial", "deprecation", "unused",
		"static-access" })
public class MedQueryFieldAddInfo implements Serializable {

	private static final long serialVersionUID = 12255555565656L;

	private String fieldCode;

	private Object fieldDefaultValue;

	private IConstEnum[] comboxData;

	private boolean blogfield = false;

	private String refFixWhere = null;

	private String refTableName;

	private IUIRefCreator refCreator;

	private ArrayList<String> pkOrgValuePowerList;

	private ArrayList<String> refPowerFieldCodeList;

	private IConstEnum[] checkBoxEditorDatas;

	private ArrayList<Object[]> listRefInfos;

	public static int RI_FIELD_CODE = 0;

	public static int RI_REF_NAME = 1;

	public static int RI_WHERE_CLAUSE = 2;

	public static int RI_FIELD_EDITOR = 3;

	public static int RI_FIELD_CURWHERE = 4;

	public MedQueryFieldAddInfo() {
		super();
	}

	public String getFieldCode() {
		return fieldCode;
	}

	public void setFieldCode(String fieldCode) {
		this.fieldCode = fieldCode;
	}

	public Object getFieldDefaultValue() {
		return fieldDefaultValue;
	}

	public void setFieldDefaultValue(Object fieldDefaultValue) {
		this.fieldDefaultValue = fieldDefaultValue;
	}

	public IConstEnum[] getComboxData() {
		return comboxData;
	}

	public void setComboxData(IConstEnum[] comboxData) {
		this.comboxData = comboxData;
	}

	public boolean isBlogfield() {
		return blogfield;
	}

	public void setBlogfield(boolean blogfield) {
		this.blogfield = blogfield;
	}

	public ArrayList<Object[]> getListRefInfos() {
		return listRefInfos;
	}

	public void addRefInfo(String fieldcode, String refname, String swhere,
			String checkfield) {
		if (fieldcode == null || checkfield == null
				|| !checkfield.equals(fieldCode))
			return;
		if (listRefInfos == null)
			listRefInfos = new ArrayList<Object[]>();
		Object[] rowinfo = null;
		for (Object[] row : listRefInfos) {
			if (row[RI_FIELD_CODE] != null
					&& fieldcode.equals(row[RI_FIELD_CODE])) {
				rowinfo = row;
				break;
			}
		}
		if (rowinfo == null) {
			rowinfo = new Object[5];
			listRefInfos.add(rowinfo);
		}
		rowinfo[RI_FIELD_CODE] = fieldcode;
		rowinfo[RI_REF_NAME] = refname;
		int pos = swhere.indexOf("=", swhere.length() - 1);
		if (swhere != null && pos > 0)
			rowinfo[RI_WHERE_CLAUSE] = swhere.substring(0, pos);
		else
			rowinfo[RI_WHERE_CLAUSE] = swhere;
		rowinfo[RI_FIELD_EDITOR] = new ArrayList<DefaultFilterEditor>();
	}

	public boolean addRefEditor(DefaultFilterEditor editor) {
		if (editor == null || listRefInfos == null)
			return false;
		if (listRefInfos == null || listRefInfos.size() <= 0)
			return false;
		ArrayList<DefaultFilterEditor> listeditor = null;
		String sfieldcode = null;
		for (Object[] row : listRefInfos) {
			sfieldcode = (String) row[MedQueryFieldAddInfo.RI_FIELD_CODE];
			if (sfieldcode != null
					&& sfieldcode.equals(editor.getFilterMeta().getFieldCode())) {
				listeditor = (ArrayList<DefaultFilterEditor>) row[MedQueryFieldAddInfo.RI_FIELD_EDITOR];
				if (listeditor == null)
					continue;
				if (!listeditor.contains(editor)) {
					listeditor.add(editor);
					return true;
				}
			}
		}
		return false;
	}

	public void proccUnUseFieldEditor() {
		if (listRefInfos == null || listRefInfos.size() <= 0)
			return;
		ArrayList<DefaultFilterEditor> listrefeditor = null;
		DefaultFilterEditor refeditor = null;
		for (Object[] refinfo : listRefInfos) {
			listrefeditor = (ArrayList<DefaultFilterEditor>) refinfo[MedQueryFieldAddInfo.RI_FIELD_EDITOR];
			if (listrefeditor == null || listrefeditor.size() <= 0)
				return;
			for (int i = listrefeditor.size() - 1; i >= 0; i--) {
				refeditor = listrefeditor.get(i);
				if (refeditor == null)
					continue;
				if (refeditor.getParent() == null)
					listrefeditor.remove(i);
			}
		}
	}

	public String getRefFixWhere() {
		return refFixWhere;
	}

	public void setRefFixWhere(String refFixWhere) {
		this.refFixWhere = refFixWhere;
	}

	public void proccFixWhere(MedTemplateInfo tinfo, String pk_corp) {
		if (tinfo == null)
			return;
		if (listRefInfos == null || listRefInfos.size() <= 0)
			return;
		String sfieldcode = null, swhere = null;
		MedQueryFieldAddInfo info = null;
		for (Object[] row : listRefInfos) {
			sfieldcode = (String) row[MedQueryFieldAddInfo.RI_FIELD_CODE];
			if (sfieldcode == null)
				continue;
			info = tinfo.getSCMQueryFieldAddInfo(sfieldcode);
			if (info == null)
				continue;
			swhere = " ";
			if (info.getRefFixWhere() != null)
				swhere = info.getRefFixWhere();
			swhere = swhere + " and ";
			if (info.getRefTableName() != null
					&& info.getRefTableName().trim().length() > 0)
				swhere = swhere + info.getRefTableName() + ".";
			swhere = swhere + "pk_corp = '" + pk_corp + "' ";
			info.setRefFixWhere(swhere);
		}
	}

	public String getRefTableName() {
		return refTableName;
	}

	public void setRefTableName(String refTableName) {
		this.refTableName = refTableName;
	}

	public IUIRefCreator getRefCreator() {
		return refCreator;
	}

	public void setRefCreator(IUIRefCreator refCreator) {
		this.refCreator = refCreator;
	}

	public ArrayList<String> getPkOrgValuePowerList() {
		return pkOrgValuePowerList;
	}

	public void addPkOrgValuePower(String[] pkvalues) {
		if (pkvalues == null || pkvalues.length <= 0)
			return;
		if (pkOrgValuePowerList == null)
			pkOrgValuePowerList = new ArrayList<String>();
		for (String pk : pkvalues)
			if (!pkOrgValuePowerList.contains(pk))
				pkOrgValuePowerList.add(pk);
	}

	public ArrayList<String> getRefPowerFieldCodeList() {
		return refPowerFieldCodeList;
	}

	public void addRefPowerFieldCode(String[] reffieldcodes) {
		if (reffieldcodes == null || reffieldcodes.length <= 0)
			return;
		if (refPowerFieldCodeList == null)
			refPowerFieldCodeList = new ArrayList<String>();
		for (String code : reffieldcodes)
			if (!refPowerFieldCodeList.contains(code))
				refPowerFieldCodeList.add(code);
	}

	public PowerCorrelationVO getPowerCorrlationVO() {
		if (refPowerFieldCodeList == null || refPowerFieldCodeList.size() <= 0)
			return null;
		PowerCorrelationVO pvo = new PowerCorrelationVO();
		pvo.setOrgFieldcode(getFieldCode());
		pvo.setOrgPks(getPkOrgValuePowerList());
		pvo.setCorrelatedFieldcode(getRefPowerFieldCodeList());
		return pvo;
	}

	public IConstEnum[] getCheckBoxEditorDatas() {
		return checkBoxEditorDatas;
	}

	public void setCheckBoxEditorDatas(IConstEnum[] checkBoxEditorDatas) {
		this.checkBoxEditorDatas = checkBoxEditorDatas;
	}

}